<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午3:24
 */
class Customer_api extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('url');
        $this->load->helper('common');
        $this->load->model('user_model');
        $this->load->model('customer_model');
        $this->load->model('admin_model');
        $this->load->library('session');
    }

    // 修改用户的折扣信息

    /**
     * @return object
     */
    public function update_user_discount()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'discount'])) {
            $update_arr['discount'] = $post['discount'];
            $bool = $this->db->where(array("shop_user_id" => $post['shop_user_id']))->update('app_customer', $update_arr);
            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "更新成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 修改用户的单品调价数

    /**
     * @return object
     */
    public function update_user_adjustments()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'adjustments', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $update_arr['adjustments'] = $post['adjustments'];
            $bool = $this->db->where(array("shop_user_id" => $post['shop_user_id']))->update('app_customer', $update_arr);
            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "更新成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城修改用户基本信息 cj
    public function update_user_base_info_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select count(1) as res from app_customer where shop_user_id = ' . $post['shop_user_id'])->row_array();
            if (!$res['res']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $update_arr = [];
            if (isset($post['name'])) {
                // 联系人
                $update_arr['name'] = $post['name'];
            }
            if (isset($post['tel'])) {
                // 联系方式
                $update_arr['tel'] = $post['tel'];
            }
            if (isset($post['email'])) {
                // 电子邮箱
                $update_arr['email'] = $post['email'];
            }
            if (isset($post['company'])) {
                // 公司名称
                $update_arr['company'] = $post['company'];
            }
            if (empty($update_arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败，内容为空"));
            }
            $bool = $this->db->where(array("shop_user_id" => $post['shop_user_id']))->update('app_customer', $update_arr);
//            $res = $this->db->affected_rows();
            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "更新成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城修改用户收货信息 cj
    public function update_user_address_info_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_id','name','province','city','district','region_id','address','phone','time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select count(1) as res from app_address where shop_id = ' . $post['shop_id'])->row_array();
            if (!$res['res']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $update_arr = [];
            if (isset($post['name'])) {
                // 收货人姓名
                $update_arr['name'] = $post['name'];
            }
            if (isset($post['province'])) {
                // 省
                $update_arr['province'] = $post['province'];
            }
            if (isset($post['city'])) {
                // 市
                $update_arr['city'] = $post['city'];
            }
            if (isset($post['district'])) {
                // 区
                $update_arr['district'] = $post['district'];
            }
            if (isset($post['region_id'])) {
                // 地址ID
                $update_arr['region_id'] = $post['region_id'];
            }
            if (isset($post['address'])) {
                // 街道地址
                $update_arr['address'] = $post['address'];
            }
            if (isset($post['postcode'])) {
                // 邮政编码
                $update_arr['postcode'] = $post['postcode'];
            }
            if (isset($post['phone'])) {
                // 手机
                $update_arr['phone'] = $post['phone'];
            }
            if (isset($post['tel'])) {
                // 固话
                $update_arr['tel'] = $post['tel'];
            }
            if (empty($update_arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败，内容为空"));
            }
            $bool = $this->db->where(array("shop_id" => $post['shop_id']))->update('app_address', $update_arr);
//            $res = $this->db->affected_rows();
            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "更新成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城新增用户收货信息 cj
    public function add_user_address_info_api()
    {
        $post = $this->input->post();
        log_message('error', var_export($post,true));
        if (check_param($post, ['shop_id', 'shop_user_id', 'name','province','city','district','region_id','address','phone','time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select id as customer_id from app_customer where shop_user_id = ' . $post['shop_user_id'])->row_array();
            if (!$res['customer_id']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $update_arr['customer_id'] = $res['customer_id'];
            if (isset($post['name'])) {
                // 收货人姓名
                $update_arr['name'] = $post['name'];
            }
            if (isset($post['province'])) {
                // 省
                $update_arr['province'] = $post['province'];
            }
            if (isset($post['city'])) {
                // 市
                $update_arr['city'] = $post['city'];
            }
            if (isset($post['district'])) {
                // 区
                $update_arr['district'] = $post['district'];
            }
            if (isset($post['region_id'])) {
                // 地址ID
                $update_arr['region_id'] = $post['region_id'];
            }
            if (isset($post['address'])) {
                // 街道地址
                $update_arr['address'] = $post['address'];
            }
            if (isset($post['postcode'])) {
                // 邮政编码
                $update_arr['postcode'] = $post['postcode'];
            }
            if (isset($post['phone'])) {
                // 手机
                $update_arr['phone'] = $post['phone'];
            }
            if (isset($post['tel'])) {
                // 固话
                $update_arr['tel'] = $post['tel'];
            }
            if (empty($update_arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败，内容为空"));
            }
            $update_arr['shop_id'] = $post['shop_id'];
            $this->db->insert('app_address', $update_arr);
            $insert_id = $this->db->insert_id();
            if ($insert_id) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "添加成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城新增用户发票信息 cj
    public function add_user_invoice_info_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_id', 'shop_user_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select id as customer_id from app_customer where shop_user_id = ' . $post['shop_user_id'])->row_array();
            if (!$res['customer_id']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $update_arr['customer_id'] = $res['customer_id'];
            if (isset($post['invoice_type'])) {
                // 发票类型(2:增值税专用发票,4:企业普通发票)
                $update_arr['invoice_type'] = $post['invoice_type'];
            }
            if (isset($post['invoice_content'])) {
                // 发票内容(1:明细,2:办公用品,3:电脑配件,4:耗材,5:学习用品,6:文具用品,7:自定义)
                $update_arr['invoice_content'] = $post['invoice_content'];
            }
            if (isset($post['invoice_company_name'])) {
                // 单位名称
                $update_arr['invoice_company_name'] = $post['invoice_company_name'];
            }
            if (isset($post['invoice_code'])) {
                // 纳税人识别码
                $update_arr['invoice_code'] = $post['invoice_code'];
            }
            if (isset($post['invoice_address'])) {
                // 注册地址
                $update_arr['invoice_address'] = $post['invoice_address'];
            }
            if (isset($post['invoice_tel'])) {
                // 注册电话
                $update_arr['invoice_tel'] = $post['invoice_tel'];
            }
            if (isset($post['invoice_bank'])) {
                // 开户银行
                $update_arr['invoice_bank'] = $post['invoice_bank'];
            }
            if (isset($post['invoice_account'])) {
                // 银行账号
                $update_arr['invoice_account'] = $post['invoice_account'];
            }
            if (isset($post['shop_id'])) {
                // 商城保存ID
                $update_arr['shop_id'] = $post['shop_id'];
            }
            if (empty($update_arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败，内容为空"));
            }
            $update_arr['shop_id'] = $post['shop_id'];
            $this->db->insert('app_invoice', $update_arr);
            $insert_id = $this->db->insert_id();
            if ($insert_id) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "添加成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城新增用户发票信息 cj
    public function update_user_invoice_info_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select count(1) as res from app_invoice where shop_id = ' . $post['shop_id'])->row_array();
            if (!$res['res']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            if (isset($post['invoice_type'])) {
                // 发票类型(2:增值税专用发票,4:企业普通发票)
                $update_arr['invoice_type'] = $post['invoice_type'];
            }
            if (isset($post['invoice_content'])) {
                // 发票内容(1:明细,2:办公用品,3:电脑配件,4:耗材,5:学习用品,6:文具用品,7:自定义)
                $update_arr['invoice_content'] = $post['invoice_content'];
            }
            if (isset($post['invoice_company_name'])) {
                // 单位名称
                $update_arr['invoice_company_name'] = $post['invoice_company_name'];
            }
            if (isset($post['invoice_code'])) {
                // 纳税人识别码
                $update_arr['invoice_code'] = $post['invoice_code'];
            }
            if (isset($post['invoice_address'])) {
                // 注册地址
                $update_arr['invoice_address'] = $post['invoice_address'];
            }
            if (isset($post['invoice_tel'])) {
                // 注册电话
                $update_arr['invoice_tel'] = $post['invoice_tel'];
            }
            if (isset($post['invoice_bank'])) {
                // 开户银行
                $update_arr['invoice_bank'] = $post['invoice_bank'];
            }
            if (isset($post['invoice_account'])) {
                // 银行账号
                $update_arr['invoice_account'] = $post['invoice_account'];
            }
            if (empty($update_arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败，内容为空"));
            }
            $bool = $this->db->where(array("shop_id" => $post['shop_id']))->update('app_invoice', $update_arr);
            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "更新成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "更新失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城修改默认地址同步 cj
    public function change_user_default_address_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_id', 'shop_user_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            if ($customer_id) {
                $res = $this->user_model->change_user_default_address($customer_id, $post['shop_id']);
                if ($res) {
                    send_json(array('code' => 0, 'data' => '', 'msg' => "同步成功"));
                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "同步失败"));
                }

            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有该用户"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城修改默认发票信息 cj
    public function change_user_default_invoice_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_id', 'shop_user_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            if ($customer_id) {
                $res = $this->user_model->change_user_default_invoice($customer_id, $post['shop_id']);
                if ($res) {
                    send_json(array('code' => 0, 'data' => '', 'msg' => "同步成功"));
                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "同步失败"));
                }

            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "没有该用户"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }


    function get_account()
    {
        $shop_user_id = $this->input->get('shop_user_id');
        if ($shop_user_id) {
            $customer_id = $this->user_model->shop_user_id_to_customer_id($shop_user_id);
            if ($customer_id) {
                $admin_customer_id = $this->customer_model->get_admin_customer_id($customer_id);
                if (!$admin_customer_id) {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "主账号错误"));
                } else {
                    $admin = $this->customer_model->query_customer_admin_info($admin_customer_id);
                    if ($admin['account_period_type'] == 1) {
                        $date = new \DateTime();
                        $date->modify('+7 day');
                        if ($date->format('d') >= $admin['account_period_date']) {
                            $date->modify('+1 month');
                            $date = new \DateTime($date->format('Y') . '-' . $date->format('m') . '-' . $admin['account_period_date']);
                        } else {
                            $date = new \DateTime($date->format('Y') . '-' . $date->format('m') . '-' . $admin['account_period_date']);
                        }
                        $data['should_repayment_date'] = $date->format('Y-m-d');
                        $account_period_settlement_time = $admin['account_period_settlement_time'] ? $admin['account_period_settlement_time'] : 0;
                        $account_period_invoice_date = $admin['account_period_invoice_date'] ? $admin['account_period_invoice_date'] : 0;
                        $s = $account_period_settlement_time + $account_period_invoice_date;
                        $date->modify(' +' . $s . ' day ');
                        $data['should_repayment_date'] = $date->format('Y-m-d');
                    } else {
                        $date = new \DateTime();
                        $date->modify('+7 day');
                        $account_period_settlement_time = $admin['account_period_settlement_time'] ? $admin['account_period_settlement_time'] : 0;
                        $date->modify(' +' . $account_period_settlement_time . ' day ');
                        $data['should_repayment_date'] = $date->format('Y-m-d');
                    }
                    if ($admin['is_account'] == 0) {
                        $data['account_status'] = 0;
                        $data['account_money'] = 0.00;
                        $data['account_money_used'] = 0.00;
                    } else {
                        if ($admin['account_period_pay_status'] == 2) {
                            $data['account_status'] = 0;
                            $data['account_money'] = 0.00;
                            $data['account_money_used'] = 0.00;
                        } else if ($admin['account_period_pay_status'] == 0) {

                            $data['should_repayment_date'] = date('Y-m-d');
                            $record = $this->db->query('select * from app_account_period_open_close_record where customer_id = ' . $customer_id . ' and bill_id is not null and type = 4 order by id desc limit 1')->row_array();
                            if ($record && $record['bill_id']) {
                                $bill = $this->db->query('select * from app_bill where id = ' . $record['bill_id'])->row_array();
                                if ($bill && $bill['should_repayment_date']) {
                                    $data['should_repayment_date'] = $bill['should_repayment_date'];
                                }
                            }

                            $data['account_status'] = 2;
                            $data['account_money'] = $admin['account_period_quota'];
                            $data['account_money_used'] = $admin['account_period_quota_used'] ? $admin['account_period_quota_used'] : 0.00;
                        } else {
                            $data['account_status'] = 1;
                            $data['account_money'] = $admin['account_period_quota'];
                            $data['account_money_used'] = $admin['account_period_quota_used'] ? $admin['account_period_quota_used'] : 0.00;
                        }
                    }
                    $customer = $this->customer_model->query_customer_info($customer_id);
                    $data['identity'] = $customer['identity'];
                    send_json(array('code' => 0, 'data' => $data, 'msg' => "获取成功"));
                }
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "用户ID错误"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }
}